<?php
/**
 * 后台菜单管理控制器
  * PHP version 5
 * @package   Antonia
 * @author    Antonia <471699715@qq.com>
 * @copyright 2016 Antonia.
 */
namespace Admin\Controller;
use Think;
class MenuController extends AdmincommonController {
    /**
     * 显示列表
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function index() {
        $Model = D("Menus");
        $this->assign('list',$Model->fetchAll());
        $this->display();
    }

    /**
     * 显示新增页面
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function add() {
        $Model = D("Menus");
        $this->assign('parentCates', $Model->getOptions());
        $this->display('info');
    }

    /**
     * 处理新增
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function insert() {
        $Model = D("Menus");
        if ($vo = $Model->create()) {
            unset($vo["menu_id"]);
            $rs = $Model->add($vo);
            if ($rs !== false) {
				$re['statusCode'] = "200";
                $re['message'] = "新增成功!";
                $re['navTabId'] = "Menuindex";
                $re['callbackType'] = "closeCurrent";
            } else {
				$re['statusCode'] = "300";
                $re['message'] = "新增错误，请重试!";
            }
        } else {
			$re['statusCode'] = "300";
            $re['message'] = $Model->getError();
        }
		echo json_encode($re);
    }

    /**
     * 显示编辑页面
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function edit() {
        $Model_id = remove_xss($_GET['id']);
        if (!empty($Model_id)) {
            $Model     = D("Menus");
            $ModelInfo = $Model->find($Model_id);
            if ($ModelInfo) {
                $this->assign('parentCates', $Model->getOptions());
                $this->assign('menu', $ModelInfo);
                $this->display('info');
            } else {
                $re['statusCode'] = "300";
                $re['navTabId'] = "Menuindex";
                $re['message'] = "该记录不存在！";
                echo json_encode($re);
            }
        } else {
			$re['statusCode'] = "300";
			$re['navTabId'] = "Menuindex";
			$re['message'] = "该记录不存在！";
			echo json_encode($re);
        }
    }

    /**
     * 处理更新数据
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function update() {
        $Model = D("Menus");
        if ($vo = $Model->create()) {
            $list = $Model->save($vo);
            if ($list !== false) {
                $re['statusCode'] = "200";
                $re['message'] = "菜单信息修改成功!";
                $re['navTabId'] = "Menuindex";
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "没有更新任何数据!";
            }
        } else {
			 $re['statusCode'] = "300";
             $re['message'] = $Model->getError();
        }
		 echo json_encode($re);
    }

    /**
     * 删除菜单
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function delete() {
        if (!empty($_GET['id'])) {
            $Model = D("Menus");
            $result = $Model->delete(remove_xss($_GET['id']));
            if (false !== $result) {
                $re['statusCode'] = "200";
                $re['message'] = "删除成功!";
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "删除失败，请重试!";
            }
        } else {
                $re['statusCode'] = "300";
                $re['message'] = "该记录不存在！";
        }
        echo json_encode($re);
    }

    /**
     * 删除选定内容
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function delAll() {
        $Model = D("Menus");
        $map['menu_id']  = array('in', explode(',', $_POST["ids"]));
        $result  = $Model->where($map)->delete();
      if($result) {
            $re['statusCode'] = "200";
            $re['message'] = "删除成功!";
        } else {
            $re['statusCode'] = "300";
            $re['message'] = "删除失败，请重试!";
        }
        echo json_encode($re);
    }
}
?>